{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5.3. Accelerating array computations with Numexpr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import numexpr as ne"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y, z = np.random.rand(3, 1000000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.94 ms ± 223 µs per loop (mean ± std. dev. of 7 runs,\n",
      "    100 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit x + (y**2 + (z*x + 1)*3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.47 ms ± 8.07 µs per loop (mean ± std. dev. of 7 runs,\n",
      "    1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit ne.evaluate('x + (y**2 + (z*x + 1)*3)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ne.ncores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.53 ms ± 12.9 µs per loop (mean ± std. dev. of 7 runs,\n",
      "    100 loops each)\n",
      "2.35 ms ± 276 µs per loop (mean ± std. dev. of 7 runs,\n",
      "    100 loops each)\n",
      "1.6 ms ± 60 µs per loop (mean ± std. dev. of 7 runs,\n",
      "    1000 loops each)\n",
      "1.5 ms ± 24.6 µs per loop (mean ± std. dev. of 7 runs,\n",
      "    1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 5):\n",
    "    ne.set_num_threads(i)\n",
    "    %timeit ne.evaluate('x + (y**2 + (z*x + 1)*3)',)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
